const db = wx.cloud.database();
const staffInfoCollection = db.collection("staff_info");
const recordsCollection = db.collection("records");

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //@todo 优化数据的异步加载问题，可以用一个 Loading


    staffInfoCollection.doc(options.id).field({
      name: true,
      department: true
    }).get().then(staffInfoResult => {

      this.setData({
        department: staffInfoResult.data.department,
        name: staffInfoResult.data.name
      }, () => {
        console.log("数据传递成功");
      })

    });

    recordsCollection.orderBy('time', 'DESC').limit(1).get().then(res => {
      this.setData(res.data[0])
    })
    
  },
  
  onSubmit:function(){
    if(this.data.checked){
      const data = {
        temperature: this.data.temperature,
        other: this.data.other,
        is_weak: this.data.is_weak,
        time: db.serverDate()
      };
      recordsCollection.add({
        data
      }).then(() => {
        wx.redirectTo({
          url: './success',
        })
      })
    }else{
      wx.showModal({
        title: '请阅读规范',
        content: '您还没有阅读规范,请阅读规范并勾选选项',
        showCancel: false
      })
    }
  },
  formData: {
    checked: false,
    is_weak: false
  },
  onWeakChange(event) {
    this.setData({
      is_weak: event.detail
    });
  },
  onBoxChange(event) {
    this.setData({
      checked: event.detail
    });
  }
})